package ex.mu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * 多线程复制文件
 * @author small瑞
 */
public class FileMu {
    public static void main(String[] args) throws IOException {

        File file = new File("G:\\IO\\mmp/鞠婧祎 - 叹云兮.mp3");

        FileInputStream fileInputStream = new FileInputStream(file);

        FileOutputStream fileOutputStream = new FileOutputStream("G:\\IO\\mmp/鞠婧祎 - 叹云兮-复制.mp3");


        LinkedBlockingQueue<Integer> bytes = new LinkedBlockingQueue<>();
        //取文件
        Runnable runnableGet = () -> {
            try {
                int read;
                while((read =fileInputStream.read())!=-1){
                    bytes.put(read);
                }
                fileInputStream.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        };

        //放文件
        Runnable runnablePut = () -> {
            try {
                int i = 0;
                while(i<file.length()){
                    int take = bytes.take();
                    fileOutputStream.write(take);
                    i++;
                }
                fileOutputStream.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        };

        //开线程

        Thread threadGet = new Thread(runnableGet);
        Thread threadPut = new Thread(runnablePut);
        threadGet.start();
        threadPut.start();





    }
}
